import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/clubs/finances/finances_controller.dart';
import 'package:flutter_changdian_football/pages/mine/my_purse/my_purse_controller.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/pull/pull_smart_refresher.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';

/// 财政
class FinancesPage extends StatelessWidget {
  const FinancesPage({
    super.key,
    this.clubId = ''
  });

  final String clubId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF8FAF9),
      // appBar: AppBar(
      //   centerTitle: true,
      //   elevation: 0,
      //   title: Text(
      //     '俱乐部财政',
      //     style: TextStyle(fontSize: 16.sp, color: Colors.black),
      //   ),
      //   iconTheme: const IconThemeData(
      //     color: Colors.black
      //   ),
      //   backgroundColor: const Color(0xffF8FAF9),
      // ),
      body: _buildScaffoldBody(context),
    );
  }


  Widget _buildScaffoldBody(context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    final double pinnedHeaderHeight = statusBarHeight + kToolbarHeight;
    return GetBuilder<MyPurseController>(
      init: MyPurseController(isCz: true, clubId: clubId),
      builder: (myPurseController) {
        return ExtendedNestedScrollView(
          headerSliverBuilder: (BuildContext c, bool f) {
            return <Widget>[
              SliverAppBar(
                pinned: true,
                expandedHeight: 180.w + kToolbarHeight,
                title: Text('俱乐部财政', style: TextStyle(fontSize: 16.sp, color:const Color(0xff333333)),),
                iconTheme: const IconThemeData(
                  color: Color(0xff333333)
                ),
                centerTitle: true,
                elevation: 0.0,
                backgroundColor: const Color(0xffF8FAF9),
                flexibleSpace: FlexibleSpaceBar(
                  collapseMode: CollapseMode.pin,
                  background: Column(
                    children: [
                      SafeArea(
                        child: Stack(
                          children: [
                            Container(
                              margin: EdgeInsets.only(top: kToolbarHeight + 20.w, left: 17.w, right: 17.w),
                              width: double.infinity,
                              height: 120.w,
                              decoration: const BoxDecoration(
                                image: DecorationImage(image: AssetImage('assets/images/img_cz_kb.png'), fit: BoxFit.fill)
                              ),
                              padding: EdgeInsets.only(left: 20.w, right: 16.w, top: 14.w),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Container(
                                    padding: EdgeInsets.only(top: 16.w),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        SizedBox(
                                          child: Text('当前余额', style: TextStyle(fontSize: 12.sp, color: Colors.white),),
                                        ),
                                        SizedBox(height: 2.w,),
                                        SizedBox(
                                          child: RichText(
                                            text: TextSpan(
                                              children: [
                                                TextSpan(
                                                  text: '￥ ',
                                                  style: TextStyle(fontSize: 15.sp, color: Colors.white)
                                                ),
                                                TextSpan(
                                                  text: '${myPurseController.userAccountBalance}',
                                                  style: TextStyle(fontSize: 32.sp, color: Colors.white, fontWeight: FontWeight.w500)
                                                )
                                              ]
                                            )
                                          ),
                                        )
                                      ],
                                    ),
                                  ),

                                  Offstage(
                                    offstage: !myPurseController.accountAccess,
                                    child: Column(
                                      children: [
                                        GestureDetector(
                                          behavior: HitTestBehavior.translucent,
                                          onTap: () {
                                            Get.toNamed(AppRoutes.recharge, parameters: {
                                              'type': CustomConstant.recharge_finance,
                                              'clubId': clubId
                                            });
                                          },
                                          child: Container(
                                            padding: EdgeInsets.symmetric(vertical: 7.w, horizontal: 20.w),
                                            decoration: BoxDecoration(
                                              color: Colors.white,
                                              borderRadius: BorderRadius.circular(18.r)
                                            ),
                                            child: Text('充值', style: TextStyle(fontSize: 15.sp, color: const Color(0xff499360 )),),
                                          ),
                                        ),

                                        SizedBox(height: 20.w,),

                                        GestureDetector(
                                          behavior: HitTestBehavior.translucent,
                                          onTap: () {
                                            Get.toNamed(AppRoutes.withdraw, parameters: {
                                              'type': CustomConstant.withdraw_finance,
                                              'clubId': clubId
                                            });
                                          },
                                          child: Container(
                                            padding: EdgeInsets.symmetric(vertical: 7.w, horizontal: 20.w),
                                            decoration: BoxDecoration(
                                              color: const Color(0xff519967 ),
                                              borderRadius: BorderRadius.circular(18.r)
                                            ),
                                            child: Text('提现', style: TextStyle(fontSize: 15.sp, color: Colors.white),),
                                          ),
                                        ),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ],
                        )
                      ),

                      SizedBox(height: 10.w,),

                      SizedBox(
                        child: Text('*只有俱乐部主席和财务能进行俱乐部资金管理', style: TextStyle(fontSize: 12.sp, color: const Color(0xffF4333C)),),
                      )
                    ],
                  ),
                ),
              )
            ];
          },
          pinnedHeaderSliverHeightBuilder: () {
            return pinnedHeaderHeight;
          },
          body: GetBuilder<FinancesController>(
            init: FinancesController(clubId: clubId),
            builder: (financesController) {
              return RefreshWidget<FinancesController>(
                child: Container(
                  color: Colors.white,
                  child: Column(
                    children: [
                      Expanded(
                        child: ListView.separated(
                          itemCount: financesController.financesRecordModelList.length,
                          padding: EdgeInsets.zero,
                          physics: const BouncingScrollPhysics(),
                          separatorBuilder: (context, index) {
                            return Container(
                              width: double.infinity,
                              height: 0.5.w,
                              margin: EdgeInsets.symmetric(horizontal: 17.w),
                              color: const Color(0xffF8FAF9 ),
                            );
                          },
                          itemBuilder: (BuildContext context, int index) {
                            return Container(
                              padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 10.w),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        SizedBox(
                                          child: Text(financesController.financesRecordModelList[index].description ?? '', style: TextStyle(fontSize: 15.sp, color: const Color(0xff333333)),),
                                        ),
              
                                        SizedBox(height: 3.w,),
              
                                        SizedBox(
                                          child: Text(financesController.financesRecordModelList[index].dateTime ?? '', style: TextStyle(fontSize: 13.sp, color: const Color(0xffcccccc)),),
                                        )
                                      ],
                                    ),
                                  ),
              
                                  SizedBox(
                                    child: Text(financesController.financesRecordModelList[index].amount ?? '', style: TextStyle(fontSize: 16.sp, color: const Color(0xff499360), fontWeight: FontWeight.w500),),
                                  )
                                ],
                              ),
                            );
                          },
                        ),
                      )
                    ],
                  ),
                ),
              );
            }
          ),
        );
      }
    );
  }


}